package cn.hloger.spider.demo.newsDemo.service;

import cn.hloger.spider.demo.chatgptDemo.bean.req.ChatApiReq;
import cn.hloger.spider.demo.chatgptDemo.bean.resp.ChatApiResp;
import cn.hloger.spider.demo.newsDemo.enums.LineEnum;
import cn.hloger.spider.demo.newsDemo.handler.news.NewsWangYiHandler;
import cn.hloger.spider.utils.ChatUtils;
import cn.hloger.spider.utils.DateUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * TODO
 *
 * @author huanglong
 * @Description TODO
 * @data 2022/9/12
 */
@Service
public class NewsApiService {

    private static String baseTitle = "\r\n【秘书小丹】日报，每天一分钟，知晓天下事！\r\n";
    private static String baseTail = "新的一天,要快乐哟~ \r\n距离交房还有%s天\r\n";
    private static String newUrl = "更多新闻咨询访问:\r\nhttps://news.hlong.top/h5/news/view ";
    private static NewsWangYiHandler newsWangYiHandler = new NewsWangYiHandler();
    private static LocalDateTime lastDate = LocalDateTime.of(2024, 4, 30, 0, 0, 0);
    private static LocalDateTime baoziLastDate = LocalDateTime.of(2022, 12, 1, 0, 0, 0);
    private static String lineStr = "%s\r\n";


    public ChatApiResp getNews(ChatApiReq req) {
        //checkreq
        if (!req.getWord().equals("日报")) {
            return ChatUtils.renderChatResp(JSONObject.toJSONString(req));
        }


        String tail = String.format(baseTail + newUrl, DateUtils.getDifferentDayByNow(lastDate));
        String content = newsWangYiHandler.getWangYiNews(baseTitle, tail, LineEnum.WECHAR);
        //组装
        return ChatUtils.renderChatResp(content);
    }

    public String view(ChatApiReq req) {
        String tail = String.format(baseTail, DateUtils.getDifferentDayByNow(lastDate));
        return newsWangYiHandler.getWangYiNews(baseTitle, tail, LineEnum.HTML);
    }

    public String baozi(ChatApiReq req) {
        String localTitle = "【包子专属新闻】，元气满满的一天开始啦！";
        String localTail = "穿越逆境,抵达繁星," + LineEnum.HTML.getCode() + "距离包子回家还有 %s 天" + LineEnum.HTML.getCode();
        localTail = String.format(localTail, DateUtils.getDifferentDayByNow(baoziLastDate));
        return newsWangYiHandler.getWangYiNews(localTitle, localTail, LineEnum.HTML);
    }
}
